package com.dominator.repository;

import com.dominFramework.core.dao.SqlDao;
import com.dominFramework.core.id.SystemIdUtils;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;
import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.T_accountPO;
import com.dominator.mybatis.dao.T_merchant_accountDao;
import com.dominator.mybatis.dao.T_merchant_accountPO;
import com.dominator.mybatis.dao.T_userPO;
import com.dominator.utils.dao.JdbcHelper;
import com.dominator.utils.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Repository
public class DaoMerchantAccount {


    @Resource
    JdbcHelper jh;


    @Autowired
    SqlDao sqlDao;

    @Autowired
    T_merchant_accountDao tMerchantAccountDao;

    /**
     * 初始添加商户账户
     * @param merchantId
     */
    public void createAccount(String merchantId){
        T_merchant_accountPO tMerchantAccountPO = new T_merchant_accountPO();
        tMerchantAccountPO.setId(SystemIdUtils.uuid());
        tMerchantAccountPO.setMerchant_id(merchantId);
        tMerchantAccountPO.setCommission(BigDecimal.ZERO);
        tMerchantAccountPO.setMargin_money(BigDecimal.ZERO);
        tMerchantAccountPO.setPoints(BigDecimal.ZERO);
        tMerchantAccountPO.setCreate_time(new Date());
        tMerchantAccountPO.setIs_valid("1");
        tMerchantAccountDao.insert(tMerchantAccountPO);
    }


    /**
     * 获取商户帐户
     * @param merchantId
     * @return
     */
    public T_merchant_accountPO getAccountMerchantId(String merchantId){
        return (T_merchant_accountPO)sqlDao.selectOne("Merchant.selectAccountByDto", Dtos.newDto("merchant_id",merchantId));
    }

    /**
     * 扣现金
     * @param
     * @return
     */
    public void subtractMoney(String merchantId, BigDecimal money) throws ApiException {
        T_merchant_accountPO tMerchantAccountPO = getAccountMerchantId(merchantId);
        if(tMerchantAccountPO.getMoney().subtract(money).compareTo(new BigDecimal(0))<1){
            throw new ApiException(ReqEnums.MERCHANT_ACC_LACK_OF_BALANCE);
        }
        tMerchantAccountPO.setMoney(tMerchantAccountPO.getMoney().subtract(money));
        tMerchantAccountDao.updateByKey(tMerchantAccountPO);
    }

    /**
     * 扣积分
     * @param
     * @return
     */
    public void subtractPoints(String merchantId, BigDecimal points) throws ApiException {
        T_merchant_accountPO tMerchantAccountPO = getAccountMerchantId(merchantId);
        if(tMerchantAccountPO.getPoints().subtract(points).compareTo(new BigDecimal(0))<1){
            throw new ApiException(ReqEnums.MERCHANT_ACC_LACK_OF_POINTS);
        }
        tMerchantAccountPO.setPoints(tMerchantAccountPO.getPoints().subtract(points));
        tMerchantAccountDao.updateByKey(tMerchantAccountPO);
    }
}
